/*
	Copyright 2010 Kwok Ho Yin

   	Licensed under the Apache License, Version 2.0 (the "License");
   	you may not use this file except in compliance with the License.
   	You may obtain a copy of the License at

    	http://www.apache.org/licenses/LICENSE-2.0

   	Unless required by applicable law or agreed to in writing, software
   	distributed under the License is distributed on an "AS IS" BASIS,
   	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   	See the License for the specific language governing permissions and
   	limitations under the License.
*/

package com.hykwok.CameraEffect;

import android.content.Context;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MyCameraSurface extends SurfaceView implements SurfaceHolder.Callback {
	private static final String TAG = "MyCameraSurface";
	
	public SurfaceHolder		m_holder = null;
	
	private MyCameraControl 	m_control = null;
	private boolean				m_close_is_called = true;
	
	public MyCameraSurface(Context context, boolean bCreateControl) {
		super(context);
		
		Log.d(TAG, "Constructor.....");
		
		if(bCreateControl) {
			CreateControl();
		}
		
		m_holder = this.getHolder();
		m_holder.addCallback(this);
		// set push buffer type such that camera driver can create a overlay screen
		m_holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}
	
	protected void finalize() {
		Close();
	}
	
	public void CreateControl() {
		if(m_control == null) {
			m_control = new MyCameraControl();
			m_close_is_called = false;
		}
	}
	
	public void Close() {
		if(m_close_is_called == false) {
			Log.d(TAG, "Close...");
		
			// stop preview first
			if(m_control.IsPreviewRun() == true) {
				m_control.stopPreview();
			}
			// then release camera
			if(m_control.IsCameraClosed() == false) {
				m_control.Close();
				m_control = null;
			}
			
			m_close_is_called = true;
		}
	}
	
	public MyCameraControl getControl() {
		return m_control;
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,	int height) {
		Log.d(TAG, "*** surfaceChanged >>>>> ***");
		Log.d(TAG, "format=" + format + ", width=" + width + ", height=" + height);
		
		if(m_control.IsCameraClosed() == false) {
			if(m_control.IsPreviewRun() == false) {				
				m_control.startPreview();
			}
		}
		
		Log.d(TAG, "*** surfaceChanged <<<<< ***");
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		Log.d(TAG, "*** surfaceCreated >>>>> ***");
		
		CreateControl();
		m_control.setHolder(holder);
		
		Log.d(TAG, "*** surfaceCreated <<<<< ***");
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		Log.d(TAG, "*** surfaceDestroyed >>>>> ***");
		if(m_control != null) {
			if(m_control.IsCameraClosed() == false) {
				// camera is opened
				if(m_control.IsPreviewRun() == true) {
					// stop preview now because surface is no longer existed
					m_control.stopPreview();
				}
			}
		}
		Log.d(TAG, "*** surfaceDestroyed <<<<< ***");
	}
}
